<?php
include_once(SYSTEM.'/Template.php');

class Controller
{
	protected $Model;
	protected $ViewData;
	protected $UserContext;
	protected $Params;
	protected $Identity;

	public function __construct($params = null)
	{
		$this->Params = $params;
	}

	public function Render($view, $layout, $model = null)
	{
		$this->Model = $model;
		$controllerName = get_class($this);
		$viewFolderOfController = substr($controllerName, 0, -10);
		
		$viewPath = VIEW.'/'.$viewFolderOfController.'/'.$view.'.php';
		$layoutPath = null;
		if($layout != null)
		{
			$layoutPath = VIEW.'/Layouts/'.$layout.'.php';
		}
		$template = new Template($viewPath, $layoutPath, $this->ViewData, $this->Model);
		$template->LoadTempate($viewPath, $layoutPath);
	}		
	
	public function RedirectToAction($controller, $action, $param = null)
	{
		$controller = $controller.'Controller';
		include_once(CONTROLLER.'/'.$controller.'.php');
		$controllerObject = new $controller($param);
		$controllerObject->$action();
	}
	
	public function Redirect($url)
	{
		header('Location:'.$url);
		exit;
	}
	
	public function ReturnJSON($object)
	{		
		header('Type:Application/JSON');
		echo json_encode($object);
		exit;
	}
	
	public function ReturnContent($content)
	{
		if(is_string($content))
		{
			echo $content;
			exit;
		}
		return null;
	}

	public function GetParams()
	{
		return $this->Params;
	}

	public function SetParams($params)
	{
		$this->Params = $params;
	}
	
	public function SetIdentity($identity)
	{
		$this->Identity = $identity;
	}
}